package com.bawei.demo_shoppingcar.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.bawei.demo_shoppingcar.R;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;



public class ReadStandardActivity extends AppCompatActivity {

    private ListView srandard_listView;
    private ArrayList<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_standard);
        srandard_listView = (ListView) findViewById(R.id.srandard_listView);

        list = new ArrayList<>();
        Log.d("readLine","ok");
        try {
            String pathName = "/sdcard/";
            String fileName = "log.txt";
            File path = new File(pathName);
            File file = new File(pathName + fileName);
            if (!path.exists()) {
                path.mkdir();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
            Log.d("readLine","ok1");
            BufferedReader br = new BufferedReader(new FileReader(file));
            Log.d("readLine","ok2");
            String line = "";

            while ((line = br.readLine()) != null) {
                list.add(line);
                Log.d("readLine",line);
            }
            srandard_listView.setAdapter(new MyBaseAdapter());
            br.close();


        } catch (Exception e) {
            Log.d("strLine", e.toString());
            e.printStackTrace();
        }


    }

    class MyBaseAdapter extends BaseAdapter {

        private TextView text;

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.read_item, parent, false);

            text = (TextView) convertView.findViewById(R.id.read_text);
            text.setText(list.get(position));

            return convertView;
        }
    }
}
